package com.htfyun.factorytest.factorytestui;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.regex.Pattern;

import com.htfyun.factorytest.R;
import com.htfyun.factorytest.common.Constants;
import com.htfyun.factorytest.config.EinkProperty;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.FileReadWrite;
import com.htfyun.factorytest.utils.SystemPropertyUtil;

public class FactoryVersionInfoActivity extends FactoryBaseActivity {
    private Button btnPass;

    @Override
    int getLayoutId() {
        return R.layout.activity_factory_version_info;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    void onInitView(@Nullable Bundle savedInstanceState) {
        btnPass = findViewById(R.id.btnPass);
        TextView versionInfo = findViewById(R.id.versionInfo);

        StringBuilder versionStr = new StringBuilder();


        String einkWfCode = SystemPropertyUtil.getSystemProperty(Constants.PV_EINK_WF_CODE).trim();
        String einkBatch = getEinkBatch(einkWfCode);

        String serialNo = EinkProperty.getSerialNumber();
        String regex = "^SN[0-9A-Za-z]{12}$";
        boolean snPass = Pattern.matches(regex, serialNo);
        serialNo += snPass ? "\t【结果Pass】" : "\t[序列号不符合规则]";

//        versionStr.append("系统固件版本：").append(EinkProperty.getProductVersion()).append("\r\n");
        versionStr.append("序列号：").append(serialNo).append("\r\n");
        versionStr.append("触摸屏固件版本：").append(FileReadWrite.readFileByLines(Constants.CAP_FW_VERSION)).append("\r\n");
        versionStr.append("电磁屏固件版本：").append(FileReadWrite.readFileByLines(Constants.EMR_FW_VERSION)).append("\r\n");
        versionStr.append("墨水屏固件版本：").append(einkWfCode.isEmpty() ? "null [不能为空]" : einkWfCode).append("\r\n");
        versionStr.append("墨水屏批次：").append(einkBatch.isEmpty() ? "null [不能为空]" : einkBatch).append("\r\n");

        String vcomVoltage = SystemPropertyUtil.getSystemProperty(Constants.PV_EINK_VCOM);
        versionStr.append("Vcom电压：").append(vcomVoltage.isEmpty() ? "null [不能为空]" : vcomVoltage).append("\r\n");
        versionStr.append("驱动版本：").append(FileReadWrite.readFileByLines(Constants.DRIVER_VERSION)).append("\n");

        versionInfo.setText(versionStr.toString());
//        DebugLog.e(versionStr.toString());
        if (!snPass || einkWfCode.isEmpty() || einkBatch.isEmpty() || vcomVoltage.isEmpty()) {
            btnPass.setEnabled(false);
        }

    }

    @Override
    void onHandleMessage(Handler handler, Message msg) {

    }

    private String getEinkBatch(String einkWfCode) {
        String batch = "?";
        int tmp = 0, base = 0, tail = 0;

        if (einkWfCode.length() > 12) {
            char[] wfCode = einkWfCode.toCharArray();
            switch (wfCode[5]) {
                case '6':
                    batch = "C";
                    break;
                case '9':
                    batch = "G";
                    break;
                case 'R':
                    batch = "R";
                    break;
                default:
                    break;
            }

            if (Character.isDigit(wfCode[7])) {
                tail = wfCode[7] - '0';
            } else if (Character.isUpperCase(wfCode[7])) {
                tmp = wfCode[6];
                if ((tmp >= 'Q') && (tmp <= 'Z')) {
                    //[Q, Z]
                    base = 629 + (tmp - 'Q') * 23;
                } else if (tmp > 'N') {
                    //(N, Q)
                    base = 0;
                } else if (tmp >= 'J') {
                    //[J, N]
                    base = 514 + (tmp - 'J') * 23;
                } else if (tmp >= 'A') {
                    //[A, H)
                    base = 330 + (tmp - 'A') * 23;
                } else {
                    base = 0;
                }

                tmp = wfCode[7];
                if ((tmp >= 'Q') && (tmp <= 'Z')) {
                    //[Q, Z]
                    tail = tmp - 'A' - 3;
                } else if (tmp > 'N') {
                    //(N, Q)
                    return "";
                } else if (tmp >= 'J') {
                    //[J, N]
                    tail = tmp - 'A' - 1;
                } else if (tmp >= 'A') {
                    //[A, H)
                    tail = tmp - 'A';
                } else
                    return "";
                DebugLog.d("base + tail = " + base + tail);
                return batch + (base + tail);

            } else return "";

            if ((wfCode[6] <= '9') && (wfCode[6] >= '0')) {
                /* 00 ~ 99 */
                tmp = (wfCode[6] - '0') * 10 + tail;
                return batch + tmp;
            } else if ((wfCode[6] <= 'H') && (wfCode[6] >= 'A')) {
                /* 100 ~ 170 */
                tmp = (wfCode[6] - 'A') * 10 + 100 + tail;
                return batch + tmp;
            } else if ((wfCode[6] <= 'N') && (wfCode[6] >= 'J')) {
                /* 180 ~ 220 */
                tmp = (wfCode[6] - 'J') * 10 + 180 + tail;
                return batch + tmp;
            } else if ((wfCode[6] <= 'Z') && (wfCode[6] >= 'Q')) {
                /* 230 ~ 320 */
                tmp = (wfCode[6] - 'Q') * 10 + 230 + tail;
                return batch + tmp;
            } else {
                return batch + "???";
            }
        }
        return "";
    }
}
